package demo.xlj.yuyi.studentapp.common;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demo.xlj.yuyi.studentapp.R;
import demo.xlj.yuyi.studentapp.base.BaseRxActivity;
import demo.xlj.yuyi.studentapp.bean.CollcetionSheTuanBean;
import demo.xlj.yuyi.studentapp.bean.HaveSheTuan;
import demo.xlj.yuyi.studentapp.myadapter.SheTuanAdapter;
import demo.xlj.yuyi.studentapp.retrofit.RetrofitClient;
import demo.xlj.yuyi.studentapp.rxjava.RxSchedulers;
import demo.xlj.yuyi.studentapp.utils.ActivityUtil;
import demo.xlj.yuyi.studentapp.utils.SharePerferencesUtils;
import demo.xlj.yuyi.studentapp.utils.ToastUtil;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * 社团风貌 界面
 */
public class CommunityStyleActivity extends BaseRxActivity {

    @BindView(R.id.ImageView_back)
    ImageView mImageViewBack;
    @BindView(R.id.TextView_Title)
    TextView mTextViewTitle;
    @BindView(R.id.RecyclerView_CommunityStyle)
    RecyclerView mRecyclerViewCommunityStyle;


    private SheTuanAdapter mSheTuanAdapter;


    private List<HaveSheTuan.InfoBean> mList_new;

    private String uid;

    private boolean isNet = true;

    private String aid;


    private String type = "0";


    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 10){
                ToastUtil.success(CommunityStyleActivity.this, "取消收藏成功", false, true);
                isNet = true;
                haveCollectionData();
            }
        }
    };



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_community_style);
        ButterKnife.bind(this);
        mTextViewTitle.setText("社团风貌");
        uid = (String) SharePerferencesUtils.get(CommunityStyleActivity.this, "name", "99", 0);
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        mList_new = new ArrayList<>();
        initRecycler();
        if ("0".equals(type)){
            havaData();
            initType_one();
        }else {
            haveCollectionData();
            initType_two();
        }
        initListener();

    }

    /**
     * 只针对用户取消自己收藏的社团
     *
     */
    private void initType_two(){

        mSheTuanAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {
                aid = mList_new.get(position).getId()+"";
                if (uid.equals("99")) {
                    ToastUtil.error(CommunityStyleActivity.this, "获取用户身份信息出错，暂时不能收藏", true, true);
                } else {
                    if (isNet) {
                        isNet = false;
                        addRxDestroy(RetrofitClient.getInstance(CommunityStyleActivity.this)
                                .getApiService()
                                .deleteCollcetionShetuan(uid, aid)
                                .compose(RxSchedulers.<CollcetionSheTuanBean>io_main())
                                .subscribe(new Consumer<CollcetionSheTuanBean>() {
                                    @Override
                                    public void accept(@NonNull CollcetionSheTuanBean collcetionSheTuanBeans) throws Exception {
                                        switch (collcetionSheTuanBeans.getStatus()) {
                                            case 10036:
                                                //表示取消收藏成功
                                                mHandler.sendEmptyMessageDelayed(10,1500);
                                                mSheTuanAdapter.setEmptyView(R.layout.recycler_loading,mRecyclerViewCommunityStyle);
                                                mList_new.clear();
                                                mSheTuanAdapter.notifyDataSetChanged();
                                                break;
                                            case 10037:
                                                //表示已经取消收藏了
                                                isNet = true;
                                                ToastUtil.error(CommunityStyleActivity.this, "系统繁忙，请稍后！", true, true);
                                                break;
                                            default:
                                                ToastUtil.error(CommunityStyleActivity.this, "请检查您的网络配置，然后重试！", true, true);
                                                isNet = true;
                                                break;
                                        }
                                    }
                                })
                        );

                    }

                }
            }
        });


    }

    /**
     * 只获取用户收藏的社团
     */
    private void haveCollectionData() {

        addRxDestroy(RetrofitClient.getInstance(CommunityStyleActivity.this)
                .getApiService()
                .getCollectionSheTuan(uid)
                .compose(RxSchedulers.<HaveSheTuan>io_main())
                .subscribe(new Consumer<HaveSheTuan>() {
                    @Override
                    public void accept(@NonNull HaveSheTuan getNoticeBean) throws Exception {
                        if (getNoticeBean.getStatus() == 10040) {
                            mList_new.addAll(getNoticeBean.getInfo());
                            mSheTuanAdapter.notifyDataSetChanged();
                        } else {
                            ToastUtil.info(CommunityStyleActivity.this, "您暂未收藏社团", true, true);
                            mSheTuanAdapter.setEmptyView(R.layout.recycler_item_empty, mRecyclerViewCommunityStyle);

                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        ToastUtil.warning(CommunityStyleActivity.this, "暂未获取到社团信息", true, true);
                        mSheTuanAdapter.setEmptyView(R.layout.recycler_item_empty, mRecyclerViewCommunityStyle);

                    }
                }));

    }

    /**
     * 普通的我的社团界面，只让他添加收藏就好
     */
    private void initType_one(){
        mSheTuanAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {

                aid = mList_new.get(position).getId()+"";

                if (uid.equals("99")) {
                    ToastUtil.error(CommunityStyleActivity.this, "获取用户身份信息出错，暂时不能收藏", true, true);
                } else {
                    if (isNet) {
                        isNet = false;
                        addRxDestroy(RetrofitClient.getInstance(CommunityStyleActivity.this)
                                .getApiService()
                                .collcetionSheTuan(uid, aid)
                                .compose(RxSchedulers.<CollcetionSheTuanBean>io_main())
                                .subscribe(new Consumer<CollcetionSheTuanBean>() {
                                    @Override
                                    public void accept(@NonNull CollcetionSheTuanBean collcetionSheTuanBean) throws Exception {
                                        switch (collcetionSheTuanBean.getStatus()) {
                                            case 10032:
                                                //表示收藏成功
                                                ToastUtil.success(CommunityStyleActivity.this, "收藏成功,请去我的社团收藏里面查看~", false, true);
                                                isNet = true;
                                                break;
                                            case 10038:
                                                //去取消收藏
                                                ToastUtil.success(CommunityStyleActivity.this, "您已经收藏,请去我的社团收藏里面查看~", false, true);
                                                isNet = true;
                                                break;
                                            default:
                                                isNet = true;
                                                ToastUtil.error(CommunityStyleActivity.this, "请检查您的网络配置，然后重试！", true, true);
                                                break;

                                        }
                                    }
                                }, new Consumer<Throwable>() {
                                    @Override
                                    public void accept(@NonNull Throwable throwable) throws Exception {
                                        ToastUtil.error(CommunityStyleActivity.this, "请检查您的网络配置，然后重试！", true, true);
                                    }
                                }));
                    }
                }

            }
        });

    }

    private void initListener() {
        mSheTuanAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ActivityUtil.openActivityFromLeftToRightByResult(CommunityStyleActivity.this, SheTuanZiXiActivity.class, "url", mList_new.get(position).getUrl());
            }
        });


    }

    private void havaData() {
        addRxDestroy(RetrofitClient.getInstance(CommunityStyleActivity.this)
                .getApiService()
                .getSheTuan()
                .compose(RxSchedulers.<HaveSheTuan>io_main())
                .subscribe(new Consumer<HaveSheTuan>() {
                    @Override
                    public void accept(@NonNull HaveSheTuan getNoticeBean) throws Exception {
                        if (getNoticeBean.getStatus() == 10017) {
                            mList_new.addAll(getNoticeBean.getInfo());
                            mSheTuanAdapter.notifyDataSetChanged();
                        } else {
                            ToastUtil.info(CommunityStyleActivity.this, "暂未获取到社团信息", true, true);
                            mSheTuanAdapter.setEmptyView(R.layout.recycler_item_empty, mRecyclerViewCommunityStyle);

                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        ToastUtil.warning(CommunityStyleActivity.this, "暂未获取到社团信息", true, true);
                        mSheTuanAdapter.setEmptyView(R.layout.recycler_item_empty, mRecyclerViewCommunityStyle);

                    }
                }));
    }

    private void initRecycler() {
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(CommunityStyleActivity.this);
        mRecyclerViewCommunityStyle.setLayoutManager(layoutManager);


        mSheTuanAdapter = new SheTuanAdapter(R.layout.community_style_item,mList_new);

        mSheTuanAdapter.setEmptyView(R.layout.recycler_loading, mRecyclerViewCommunityStyle);

        mRecyclerViewCommunityStyle.setAdapter(mSheTuanAdapter);
    }

    @OnClick(R.id.ImageView_back)
    public void onViewClicked() {
        CommunityStyleActivity.this.finish();
    }
}
